/*************************************************************************
 * Name:
 * Email:
 *
 * Compilation:  javac Point.java
 * Execution:
 * Dependencies: StdDraw.java
 *
 * Description: An immutable data type for points in the plane.
 *
 *************************************************************************/

import java.util.Comparator;

public class Point implements Comparable<Point> {

    // compare points by slope
    public final Comparator<Point> SLOPE_ORDER = new BySlope();       // YOUR DEFINITION HERE

    private final int x;                              // x coordinate
    private final int y;                              // y coordinate

    // create the point (x, y)
    public Point(int x, int y) {
        /* DO NOT MODIFY */
        this.x = x;
        this.y = y;
    }

    // plot this point to standard drawing
    public void draw() {
        /* DO NOT MODIFY */
        StdDraw.point(x, y);
    }

    // draw line between this point and that point to standard drawing
    public void drawTo(Point that) {
        /* DO NOT MODIFY */
        StdDraw.line(this.x, this.y, that.x, that.y);
    }

    // slope between this point and that point
    public double slopeTo(Point that) {
        //same points
        if ((x == that.x) && (y == that.y))
            return Double.NEGATIVE_INFINITY;
        //hotizontal case
        if (x == that.x)
            return Double.POSITIVE_INFINITY;
        //vertical case
        if ((y == that.y))
            return 0.0;
        //general case
        return 1.0 * (that.y - y) / (that.x - x);
    }

    // is this point lexicographically smaller than that one?
    // comparing y-coordinates and breaking ties by x-coordinates
    public int compareTo(Point that) {
        //equal
        if ((x == that.x) && (y == that.y))
            return 0;
        //lower
        if (y < that.y) {
            return -1;
        } else {
            if (y == that.y)
                if (x < that.x)
                    return -1;
                else
                    return 1;
            else
                return 1;
        }
    }

    // return string representation of this point
    public String toString() {
        /* DO NOT MODIFY */
        return "(" + x + ", " + y + ")";
    }

    // unit test
    public static void main(String[] args) {
        /* YOUR CODE HERE */
    }


    private class BySlope implements Comparator<Point> {
        @Override
        public int compare(Point firstPoint, Point secondPoint) {
            Double slopeToFirstPoint = slopeTo(firstPoint);
            Double slopeToSecondPoint = slopeTo(secondPoint);

            //normal case
            if (slopeToFirstPoint > slopeToSecondPoint)
                return 1;
            else if (slopeToFirstPoint.equals(slopeToSecondPoint))
                return 0;
            else
                return -1;
        }
    }
}
